﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace EasyPiano
{
    public partial class LayoutManagerForm : Form
    {
        public List<KeyMusic> layout=new List<KeyMusic>();
        public string savePath = "";

        public LayoutManagerForm()
        {
            InitializeComponent();
            sfd_layout.InitialDirectory = "Layout";
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            AddKeyForm form = new AddKeyForm();
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                KeyMusic keyMusic = form.result;
                layout.Add(keyMusic);
                dgv_key_sound.Rows.Add(keyMusic.key.ToString(), keyMusic.index, GlobalData.soundsPath[keyMusic.index]);
            }
        }

        void SaveLayout(string path, List<KeyMusic> keyMusics)
        {
            //创建XML文档对象
            XmlDocument doc = new XmlDocument();
            //创建第一行描述信息，并且添加到doc文档中
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //创建根节点
            XmlElement layout = doc.CreateElement("Layout");
            //将根节点添加到文档中
            doc.AppendChild(layout);

            foreach (var data in keyMusics)
            {
                //给根节点Books创建第一个子节点
                XmlElement keyMusic = doc.CreateElement("KeyMusic");
                //将book1添加到根节点
                layout.AppendChild(keyMusic);

                //给book1添加三个子节点
                XmlElement key = doc.CreateElement("Key");
                key.InnerText = data.key.ToString();
                keyMusic.AppendChild(key);

                XmlElement fileName = doc.CreateElement("Index");
                fileName.InnerText = data.index.ToString();
                keyMusic.AppendChild(fileName);
            }

            doc.Save(path);
        }


        private void LayoutManagerForm_Load(object sender, EventArgs e)
        {
            foreach (var keyMusic in layout)
            {
                dgv_key_sound.Rows.Add(keyMusic.key.ToString(), keyMusic.index.ToString(), GlobalData.soundsPath[keyMusic.index]);
            }
        }

        private void bt_update_Click(object sender, EventArgs e)
        {
            AddKeyForm form = new AddKeyForm();
            foreach(DataGridViewRow row in dgv_key_sound.SelectedRows)
            {
                if (row.Cells[0].Value != null)
                {
                    string key = row.Cells[0].Value.ToString();
                    int sound = int.Parse(row.Cells[1].Value.ToString());
                    KeyMusic keyMusic = new KeyMusic((Keys)Enum.Parse(typeof(Keys), key), sound);
                    int index = layout.FindIndex(delegate (KeyMusic target) {
                        return target.key == keyMusic.key && target.index == keyMusic.index;
                    });
                    form.result = keyMusic;
                    form.ShowDialog();
                    if (form.DialogResult == DialogResult.OK)
                    {
                        layout[index] = form.result;
                        dgv_key_sound.Rows[index].Cells[0].Value = form.result.key;
                        dgv_key_sound.Rows[index].Cells[1].Value = form.result.index.ToString();
                        dgv_key_sound.Rows[index].Cells[2].Value = GlobalData.soundsPath[form.result.index];
                    }
                }
            }
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dgv_key_sound.SelectedRows)
            {
                if (row.Cells[0].Value != null)
                {
                    string key = row.Cells[0].Value.ToString();
                    int sound = int.Parse(row.Cells[1].Value.ToString());
                    KeyMusic keyMusic = new KeyMusic((Keys)Enum.Parse(typeof(Keys), key), sound);
                    int index = layout.FindIndex(delegate (KeyMusic target) {
                        return target.key == keyMusic.key && target.index == keyMusic.index;
                    });
                    layout.RemoveAt(index);                   
                }
            }
            DataGridViewRow[] rows = new DataGridViewRow[dgv_key_sound.SelectedRows.Count];
            dgv_key_sound.SelectedRows.CopyTo(rows, 0);
            foreach (var item in rows)
            {
                dgv_key_sound.Rows.Remove(item);
            }            
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            sfd_layout.InitialDirectory = System.Environment.CurrentDirectory + "\\Layout";
            if (sfd_layout.ShowDialog() == DialogResult.OK)
            {
                string file = sfd_layout.FileName;
                savePath = file;
                SaveLayout(file, layout);
            }
        }

        private void bt_apply_Click(object sender, EventArgs e)
        {
            GlobalData.keyMusics = this.layout;
            GlobalData.UpdateKeyMusicsMap();
            if (GlobalData.keyboardView == 1)
            {
                var panel = this.Owner.Controls.Find("pan_keyboard", false)[0];
                var kbc = (KeyboardControl)panel.Controls.Find("kbc", false)[0];
                kbc.ShowTheme2();
            }
            GlobalData.layoutPath = savePath;
            MessageBox.Show("应用成功！","提示");
        }
    }
}
